//app.js
const api = require('./utils/request.js');
App({
  globalData: {
    userInfo: null,
    openId: '',
    nickName: '',
    callback: ''
  },
  onLaunch: function () {
    // this.is_auth()
    
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
    //       wx.getUserInfo({
    //         success: res => {
    //           this.globalData.userInfo = res.userInfo;
    //           this.globalData.nickName = res.userInfo.nickName;
    //           this.user_login(this.globalData.nickName)
    //           // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //           // 所以此处加入 callback 以防止这种情况
    //           if (this.userInfoReadyCallback) {
    //             this.userInfoReadyCallback(res)
    //           }
    //         }
    //       })
    //     }
    //   }
    // })
  },
  /**
   * 是否授权用户信息
   */
  is_auth() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          console.log('没通过===', res)
          wx.redirectTo({
            url: '/pages/authorize/authorize',
          })
        }
      }
    })
  },
  /**
    * 用户登录
    */
  user_login(nikeName) {
    const that = this;
    wx.login({
      success: function (res) {
        const params = {
          code: res.code,
          nikeName
        };
        api._post('/woman/api/user/login', JSON.stringify(params)).then(res => {
          if (res.code === "0") {
            that.openId = res.data.openId;
            wx.setStorageSync('openId', that.openId);
            wx.redirectTo({
              url: '/pages/setstate/setstate',
            })
            if (that.callback) {
                that.callback(res)
            }
          } else {
            console.log(res);
          }
        })
      }
    });
  },
  get_open_id () {
    const that = this;
    return that.openId || wx.getStorageSync('openId') || 0;
  },
  getInoculationStatus() {
    return wx.getStorageSync('inoculationStatus');
  },
  setBarTitle (title) {
    wx.setNavigationBarTitle({title});
  },
  toast (title) {
    wx.showToast({
      title,
      icon:'none'
    });
    // setTimeout(() => {
    //   wx.hideToast()
    // }, 1500)
  }
})